Take-home Exercise 3

Class notes and Pareto Chart exercise

Frostbear https://sg.linkedin.com/in/farahfoo (SMU Masters in IT business (Fintech and Analytics))https://scis.smu.edu.sg/master-it-business
2022-01-29

To load packages required today:

packages = c('tidyverse', 'readxl', 'ggthemes', 'ggiraph', 'plotly', 
             'gganimate', 'patchwork', 'DT', 'gifski', 'gapminder')

for(p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p, character.only = T)
}

exam_data <- read_csv("data/Exam_data.csv")
head(exam_data,5)
# A tibble: 5 x 7
  ID         CLASS GENDER RACE    ENGLISH MATHS SCIENCE
  <chr>      <chr> <chr>  <chr>     <dbl> <dbl>   <dbl>
1 Student321 3I    Male   Malay        21     9      15
2 Student305 3I    Female Malay        24    22      16
3 Student289 3H    Male   Chinese      26    16      16
4 Student227 3F    Male   Chinese      27    77      31
5 Student318 3I    Male   Malay        27    11      25
p <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(
aes(tooltip = ID),
stackgroups = TRUE,
binwidth =1,
method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)


girafe(ggobj = p,
width_svg = 6,
height_svg = 6*0.618)

changing the mouse-over to show associated elements

colnames (exam_data)
[1] "ID"      "CLASS"   "GENDER"  "RACE"    "ENGLISH" "MATHS"  
[7] "SCIENCE"
p <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(aes(data_id = CLASS),
stackgroups = TRUE, binwidth =1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
  #hover_css = "fill: blue"


girafe(ggobj = p,
width_svg = 6,
height_svg = 6*0.618)

varying more of the mouse-over colours

head(p,2)
$data
# A tibble: 322 x 7
   ID         CLASS GENDER RACE    ENGLISH MATHS SCIENCE
   <chr>      <chr> <chr>  <chr>     <dbl> <dbl>   <dbl>
 1 Student321 3I    Male   Malay        21     9      15
 2 Student305 3I    Female Malay        24    22      16
 3 Student289 3H    Male   Chinese      26    16      16
 4 Student227 3F    Male   Chinese      27    77      31
 5 Student318 3I    Male   Malay        27    11      25
 6 Student306 3I    Female Malay        31    16      16
 7 Student313 3I    Male   Chinese      31    21      25
 8 Student316 3I    Male   Malay        31    18      27
 9 Student312 3I    Male   Malay        33    19      15
10 Student297 3H    Male   Indian       34    49      37
# ... with 312 more rows

$layers
$layers[[1]]
mapping: data_id = ~CLASS 
geom_interactive_dotplot: binaxis = x, stackdir = up, stackratio = 1, dotsize = 1, stackgroups = TRUE, na.rm = FALSE, .ipar = c("data_id", "tooltip", "onclick", "hover_css", "selected_css", "tooltip_fill")
stat_bindot: binaxis = x, binwidth = 1, binpositions = bygroup, method = histodot, origin = NULL, right = TRUE, width = 0.9, drop = FALSE, na.rm = FALSE
position_identity 
colnames (exam_data)
[1] "ID"      "CLASS"   "GENDER"  "RACE"    "ENGLISH" "MATHS"  
[7] "SCIENCE"
p <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(aes(data_id = CLASS),
stackgroups = TRUE, binwidth =1, method = "histodot") +
scale_y_continuous(NULL, breaks = NULL)
  #hover_css = "fill: blue"


girafe(ggobj = p,
width_svg = 6,
height_svg = 6*0.618,
options = list(opts_hover(css = "fill: blue"),
               opts_hover_inv(css = "opacity:0.2;")))

use patchwork to arrange multiple graphs together

p1 <- ggplot(data=exam_data,
aes(x = MATHS)) + 
geom_dotplot_interactive(aes(data_id = ID),
stackgroups = TRUE, binwidth =1, method = "histodot") +
  coord_cartesian (xlim=c(0,100)) +
scale_y_continuous(NULL, breaks = NULL)

p2 <- ggplot(data=exam_data,
aes(x = ENGLISH)) + 
geom_dotplot_interactive(aes(data_id = ID),
stackgroups = TRUE, binwidth =1, method = "histodot") +
  coord_cartesian (xlim=c(0,100)) +
scale_y_continuous(NULL, breaks = NULL)

# girafe(ggobj = print (p1 / p2),
# width_svg = 6,
# height_svg = 6,
# options = list(opts_hover(css = "fill: blue"),
#                opts_hover_inv(css = "opacity:0.2;")))

Using plot_ly()

setcolour <- c ("red", "blue", "green", "yellow")

plot_ly(data =exam_data,
x = ~MATHS,
y = ~ENGLISH, 
color = ~RACE,
colors = setcolour)
ggplotly (graph)

Showing data table on selected data points

d <- highlight_key(exam_data)
graph <- ggplot(d,
                aes(x= MATHS, y = ENGLISH)) +
  geom_point(dotsize = 1, color = "blue") +
  coord_cartesian(xlim=c(0,100), ylim = c(0,100))

graphhighlight <- highlight(ggplotly(graph),
                  "plotly_selected")


library (crosstalk)
crosstalk::bscols (graphhighlight, 
               DT::datatable(d), widths = 5)

gganimate: transition over time, state, reveal, events, filters, layers